
package org.sopt.dev.ImgKnight.monitor.dialog;

import java.net.InetSocketAddress;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.sopt.dev.ImgKnight.monitor.resource.ResourceManager;


public class ConnectionDialog extends Dialog {

	private Display display = null;
	
	private Shell parent = null;
	
	private Shell shell = null;
	
	private Text ipText = null;
	
	private Text portText = null;
	
	private InetSocketAddress servAddress = null;
	
	public ConnectionDialog(Display display, Shell parent) {
		this(display, parent, SWT.DEFAULT);
	}
	
	public ConnectionDialog(Display display, Shell parent, int style) {
		super(parent, style);
		this.display = display;
		this.parent = parent;
	}
	
	public void open(String entryAddress, String portNum) {
		shell = new Shell(parent, SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
		shell.setText("CrawlerMonitor");
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = gridLayout.marginWidth = 0;
		gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
		gridLayout.numColumns = 1;
		shell.setLayout(gridLayout);
		
		Label logoLabel = new Label(shell, SWT.PUSH);
		logoLabel.setImage(ResourceManager.loadImage(display, "startupLogo.bmp"));

		Group serverHostArea = new Group(shell, SWT.SHADOW_NONE);
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		serverHostArea.setLayout(gridLayout);
		serverHostArea.setText("Connect to ImgKnight-Crawler Instance");
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gridData.verticalIndent = 15;
		gridData.widthHint = 250;
		serverHostArea.setLayoutData(gridData);
		
		Label servHostAddr = new Label(serverHostArea, SWT.PUSH);
		servHostAddr.setText("Server Host:");
		ipText = new Text(serverHostArea, SWT.BORDER);
		gridData = new GridData();
		gridData.widthHint = 150;
		ipText.setLayoutData(gridData);
		if( entryAddress != null )
			ipText.setText(entryAddress);

		Label servHostPort = new Label(serverHostArea, SWT.PUSH);
		servHostPort.setText("Port:");
		portText = new Text(serverHostArea, SWT.BORDER);
		portText.setLayoutData(gridData);
		gridData = new GridData();
		gridData.widthHint = 50;
		portText.setLayoutData(gridData);
		if( portNum != null )
			portText.setText(portNum);
		
		Composite comp = new Composite(shell, SWT.NONE);
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		comp.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gridData.verticalIndent = 5;
		gridData.widthHint = 260;
		comp.setLayoutData(gridData);
		
		Button okBtn = new Button(comp, SWT.PUSH);
		okBtn.setText("OK");
		gridData = new GridData();
		gridData.horizontalIndent = 80;
		gridData.widthHint = 80;
		okBtn.setLayoutData(gridData);
		okBtn.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent event) {
				try {
					servAddress = new InetSocketAddress(ipText.getText(), Integer.parseInt(portText.getText()));
				} catch (Exception e) {
					servAddress = null;
				}
				shell.close();
			}
		});
		
		Button cancelBtn = new Button(comp, SWT.PUSH);
		cancelBtn.setText("Cancel");
		gridData = new GridData();
		gridData.horizontalIndent = 5;
		gridData.widthHint = 80;
		cancelBtn.setLayoutData(gridData);
		cancelBtn.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		
		Label copyright = new Label(shell, SWT.NONE);
		copyright.setText("Copyright (c) 2009 sopt. All Rights Reserved.");
		gridData = new GridData();
		gridData.horizontalIndent = 5;
		gridData.verticalIndent = 5;
		gridData.heightHint = 25;
		copyright.setLayoutData(gridData);
		
		shell.pack();
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	
	public InetSocketAddress getServAddress() {
		return servAddress;
	}
	
}

